package com.rm.zeyou.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.entity.TechnicianSos;
import com.rm.zeyou.mapper.DictConfigMapper;
import com.rm.zeyou.mapper.TechnicianSosMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.TechnicianSosService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.websocket.server.WebSocketServer;
import com.rm.zeyou.util.websocket.server.vo.MessageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 技师求救表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-04-19 13:44:40
 */
@Service
public class TechnicianSosServiceImpl extends ServiceImpl<TechnicianSosMapper, TechnicianSos> implements TechnicianSosService {
    @Resource
    private TechnicianSosMapper technicianSosMapper;
    @Resource
    private DictConfigMapper dictConfigMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> technicianSos(Technician technician) throws IOException {
        //给后台通知
        MessageVo messageVo = new MessageVo();
        messageVo.setMessage("你有一条求救信息, 请及时处理");
        messageVo.setNotifyTime(new Date());
        messageVo.setType(NumberConstants.TWO_CONSTANTS);
        messageVo.setTechnicianName(technician.getTechnicianName());
        messageVo.setTechnicianTel(technician.getTechnicianTel());
        String backendSosTone = dictConfigMapper.selectValue("backend_sos_tone");
        messageVo.setVoiceUrl(backendSosTone);
        //websocket发送消息到后台
        new WebSocketServer().appointSending(JSON.toJSONString(messageVo),"0" );
        //添加求救记录
        TechnicianSos technicianSos = new TechnicianSos();
        technicianSos.setTid(technician.getId());
        technicianSos.setTechnicianName(technician.getTechnicianName());
        technicianSos.setTechnicianTel(technician.getTechnicianTel());
        technicianSos.setTechnicianSosAddress(technician.getCurrentLocation());
        int insert = technicianSosMapper.insert(technicianSos);

        return ApiRestResponse.success(insert);
    }

    @Override
    public ApiRestResponse<Integer> technicianSosUploadVoice(Technician technician, String voiceUrl) {
        Integer result;
        TechnicianSos technicianSos = technicianSosMapper.selectOne(new LambdaQueryWrapper<TechnicianSos>().eq(TechnicianSos::getTid, technician.getId()).orderByDesc(TechnicianSos::getCreateTime)
                .last(" limit 1"));
        if (!CheckEmptyUtil.isEmpty(technicianSos)) {
            if (CheckEmptyUtil.isEmpty(technicianSos.getTechnicianVoiceUrl())) {
                technicianSos.setTechnicianVoiceUrl(voiceUrl);
            } else {
                technicianSos.setTechnicianVoiceUrl(technicianSos.getTechnicianVoiceUrl() + "," + voiceUrl);
            }
            result = technicianSosMapper.updateById(technicianSos);
        } else {
            //添加求救记录
            TechnicianSos insertTechnicianSos = new TechnicianSos();
            insertTechnicianSos.setTechnicianName(technician.getTechnicianName());
            insertTechnicianSos.setTechnicianTel(technician.getTechnicianTel());
            insertTechnicianSos.setTechnicianSosAddress(technician.getCurrentLocation());
            insertTechnicianSos.setTechnicianVoiceUrl(voiceUrl);
            insertTechnicianSos.setTid(technician.getId());
            result = technicianSosMapper.insert(insertTechnicianSos);
        }
        return ApiRestResponse.success(result);
    }
}
